package Model;

import java.io.*;
import java.util.Date;
import java.util.HashMap;

import javax.swing.JDialog;
import javax.swing.JLabel;

import View.Interface;

public final class Contact { //If no inheritance : make it final
	
	public HashMap<String, String> listContactField;
	public String dateAdded;

	public Contact() {
		listContactField = new HashMap<String, String>();
		
		String firstName = new String();
		String lastName = new String();
		String homeAddress = new String();
		String companyAddress = new String();
		String personalMobilePhone = new String();
		String personalHomePhone = new String();
		String proMobilePhone = new String();
		String proOfficePhone = new String();
		String homePage = new String();
		String proEmail = new String();
		String personalEmail = new String();
		String otherIM = new String();
		String otherSkype = new String();
		String group = new String();
		String picturePath = new String();
		
		listContactField.put(Model.FIRST_NAME, firstName);
		listContactField.put(Model.LAST_NAME, lastName);
		listContactField.put(Model.HOME_ADDRESS, homeAddress);
		listContactField.put(Model.COMPANY_ADDRESS, companyAddress);
		listContactField.put(Model.PERSONAL_MOBILE_PHONE, personalMobilePhone);
		listContactField.put(Model.PERSONAL_HOME_PHONE, personalHomePhone);
		listContactField.put(Model.PRO_MOBILE_PHONE, proMobilePhone);
		listContactField.put(Model.PRO_OFFICE_PHONE, proOfficePhone);
		listContactField.put(Model.HOMEPAGE, homePage);
		listContactField.put(Model.PRO_EMAIL, proEmail);
		listContactField.put(Model.PERSONAL_EMAIL, personalEmail);
		listContactField.put(Model.OTHER_IM, otherIM);
		listContactField.put(Model.OTHER_SKYPE, otherSkype);
		listContactField.put(Model.GROUP, group);
		listContactField.put(Model.PICTURE_PATH, picturePath);
		
	}
	
	public void saveContact() {
		Date d = new Date();
		dateAdded = d.toString();
		File file;
		file = new File("contacts.csv");
		FileInputStream fileInput;
		try {
			String write = new String();
			write = listContactField.get(Model.FIRST_NAME) + ";" 
			+ listContactField.get(Model.LAST_NAME) + ";" 
			+ listContactField.get(Model.HOME_ADDRESS) + ";" 
			+ listContactField.get(Model.COMPANY_ADDRESS) + ";" 
			+ listContactField.get(Model.PERSONAL_MOBILE_PHONE) + ";" 
			+ listContactField.get(Model.PERSONAL_HOME_PHONE) + ";"
			+ listContactField.get(Model.PRO_MOBILE_PHONE) + ";" 
			+ listContactField.get(Model.PRO_OFFICE_PHONE) + ";"
			+ listContactField.get(Model.HOMEPAGE) + ";" 
			+ listContactField.get(Model.PRO_EMAIL) + ";" 
			+ listContactField.get(Model.PERSONAL_EMAIL) + ";"
			+ listContactField.get(Model.OTHER_IM) + ";" 
			+ listContactField.get(Model.OTHER_SKYPE) + ";" 
			+ dateAdded + ";" 
			+ listContactField.get(Model.GROUP) + ";" 
			+ listContactField.get(Model.PICTURE_PATH) + ";";
			String personInfo = new String();
			personInfo = listContactField.get(Model.FIRST_NAME) + ";" 
					+ listContactField.get(Model.LAST_NAME) + ";" 
					+ listContactField.get(Model.HOME_ADDRESS) + ";" 
					+ listContactField.get(Model.COMPANY_ADDRESS) + ";" 
					+ listContactField.get(Model.PERSONAL_MOBILE_PHONE) + ";" 
					+ listContactField.get(Model.PERSONAL_HOME_PHONE) + ";"
					+ listContactField.get(Model.PRO_MOBILE_PHONE) + ";" 
					+ listContactField.get(Model.PRO_OFFICE_PHONE) + ";"
					+ listContactField.get(Model.HOMEPAGE) + ";" 
					+ listContactField.get(Model.PRO_EMAIL) + ";" 
					+ listContactField.get(Model.PERSONAL_EMAIL) + ";"
					+ listContactField.get(Model.OTHER_IM) + ";" 
					+ listContactField.get(Model.OTHER_SKYPE) + ";";
			System.out.println(write);
			fileInput = new FileInputStream("contacts.csv");
			BufferedReader br = new BufferedReader(new InputStreamReader(new DataInputStream(fileInput)));
			String read = new String();
			read = br.readLine(); //Skip first line
			boolean alreadyContact = false;
			while ((read = br.readLine()) != null)
        	{
        		if (read.contains(personInfo))
        		{
        			if (read.contains(listContactField.get(Model.PICTURE_PATH)))
        			{
	        			JDialog popup = new JDialog();
	        			popup.add(new JLabel("Contact Already exists"));
	        			popup.setSize(200,100);
	        			popup.setLocationRelativeTo(null);
	        			popup.setVisible(true);
	        			alreadyContact = true;
        			}
        		}
        	}
			br.close();
			fileInput.close();
			
			if (!alreadyContact)
			{
        	PrintWriter printerOut = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
			printerOut.println(write);
			printerOut.close();
			}
			Model.LAST_NO_CONTACT++;
			
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
	}

	public void delete(String contactDeleteDateAdded) {
		System.out.println(contactDeleteDateAdded);
		File fileInput = new File("contacts.csv");
		File tempFile = new File("contactsTemp.csv");
		try {
			BufferedReader br = new BufferedReader(new FileReader(fileInput));
			BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile));
			PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
			String read = new String();
			read = br.readLine(); //Skip first line
			pw.println(read);
			while ((read = br.readLine()) != null)
	    	{
	    		if (!read.contains(contactDeleteDateAdded))
	    		{
	    			pw.println(read);
	    			pw.flush();
	    		}
	    		else System.out.println("found");
	    	}
			br.close();
			bw.close();
			pw.close();
			tempFile.renameTo(fileInput);
			System.out.println("deleted");
			Model.LAST_NO_CONTACT--;
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public void modify(String contactModifyDateAdded)
	{
		
		delete(contactModifyDateAdded);
		
		saveContact();
	}
	
	//TODO: Stocker photo dans un dossier
	
	
}
